<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\store\shop;

use app\common\model\store\shop\ClerkCapital as ClerkCapitalModel;
use think\facade\Db;

/**
 * 分销商资金明细模型
 * Class Capital
 * @package app\store\model\dealer
 */
class ClerkCapital extends ClerkCapitalModel
{
    public function getList(array $param = [])
    {
        // 检索查询条件
        $filter = $this->getFilter($param);

        // 查询列表记录
        $data['list'] = $this->with(['order'])
            ->where($filter)

            ->order(['create_time' => 'desc'])
            ->paginate(15);
        $data['money'] = Db::name('store_shop_clerk_capital')->where($filter)->sum('money');
        return $data;
    }

    /**
     * 设置检索查询条件
     * @param array $param
     * @return array
     */
    private function getFilter(array $param = []): array
    {
        // 默认参数
        $params = $this->setQueryDefaultValue($param, [
            'searchType'  => '', // 关键词类型 (10订单号 20分销商ID 30会员ID)
            'searchValue' => '', // 关键词内容
            'clerkId'     => -1, // 是否已结算佣金
            'betweenTime' => [], // 起止时间
        ]);
        // 检索查询条件
        $filter = [];

        // 起止时间
        if (! empty($params['betweenTime'])) {
            $times    = between_time($params['betweenTime']);
            $filter[] = ['create_time', '>=', $times['start_time']];
            $filter[] = ['create_time', '<', $times['end_time'] + 86400];
        }

        $params['clerkId'] > -1 && $filter[] = ['clerk_id', '=', $params['clerkId']];

        return $filter;
    }

}
